package jcommon.chap5_collection.iterator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import jcommon.beans4ex.EarthQuake;

import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.iterators.ArrayListIterator;
import org.apache.commons.collections.iterators.FilterIterator;
import org.apache.commons.collections.iterators.LoopingIterator;
import org.apache.commons.collections.iterators.UniqueFilterIterator;

public class Iteration {

	public static void main(String[] args) {
		System.out.println("-------- Test LoopingIterator --------");
		loopingIteratorDemo();
		System.out.println("-------- Test ArrayListIterator --------");
		arrayListIteratorDemo();
		System.out.println("-------- Test FilterIterator --------");
		filterIteratorDemo();
		System.out.println("-------- Test UniqueIterator --------");
		uniqueFilterIteratorDemo();
	}

	private static void loopingIteratorDemo() {
		System.out
				.println("-------- A loopingIterator never stops iterating; hasNext() always return true if the collection contains values --------");
		List drivers = new ArrayList();
		drivers.add("Chris");
		drivers.add("Sean");
		drivers.add("Kathy");

		LoopingIterator loopingIterator = new LoopingIterator(drivers);
		for (int i = 1; i <= 5; i++) {
			String driver = (String) loopingIterator.next();
			System.out.println("Driver for Day " + i + ": " + driver);
		}
	}

	private static void arrayListIteratorDemo() {
		String[] strings = new String[] { "A", "B", "C", "D", "E", "F" };
		// Iterate over all elements
		ArrayListIterator iterator1 = new ArrayListIterator(strings);
		// Iterate over "C", "D", "E", "F"
		ArrayListIterator iterator2 = new ArrayListIterator(strings, 2);
		// Iterate over "B", "C", "D"
		ArrayListIterator iterator3 = new ArrayListIterator(strings, 1, 4);

		System.out.println();
		t2_printIterator(iterator1);
		System.out.println();
		t2_printIterator(iterator2);
		System.out.println();
		t2_printIterator(iterator3);
	}

	private static void t2_printIterator(ArrayListIterator iter) {
		while (iter.hasNext()) {
			int index = iter.nextIndex();
			String element = (String) iter.next();
			System.out.println("Element at " + index + ": " + element);
		}
	}

	private static void filterIteratorDemo() {
		List quakes = new ArrayList();
		EarthQuake quake1 = new EarthQuake();
		quake1.setLocation("Chicago, IL");
		quake1.setIntensity(new Float(6.4f));
		quake1.setTime(new Date());
		quakes.add(quake1);
		EarthQuake quake2 = new EarthQuake();
		quake2.setLocation("San Francisco, CA");
		quake2.setIntensity(new Float(4.4f));
		quake2.setTime(new Date());
		quakes.add(quake2);
		Predicate majorQuakePredicate = new MajorQuakePredicate(new Float(5.0),
				new Float(1000.0));
		Iterator majorQuakes = new FilterIterator(quakes.iterator(),
				majorQuakePredicate);

		while (majorQuakes.hasNext()) {
			EarthQuake quake = (EarthQuake) majorQuakes.next();
			System.out.println("ALERT! MAJOR QUAKE: " + quake.getLocation()
					+ ": " + quake.getIntensity());
		}
	}

	private static void uniqueFilterIteratorDemo() {
		String[] medals = { "tin", "gold", "gold", "silver", "silver", "silver", "platinum" };
		Iterator iter = Arrays.asList(medals).iterator();
		
		UniqueFilterIterator uniqueFilterIterator = new UniqueFilterIterator(iter);
		while(uniqueFilterIterator.hasNext()) {
			System.out.println(uniqueFilterIterator.next());
		}
	}
}
